/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.controllers;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.domain.Customer;
import org.domain.Item;
import org.domain.ItemShippingStatusList;
import org.helper.RequestCatalog;
import org.helper.ShipGoods;
import org.utilities.Logger;

/**
 *
 * @author Shaun McCready
 */
public class paymentProcess extends HttpServlet {
   
    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        ArrayList<Item> order = new ArrayList<Item>();
        ArrayList<Item> catalog = new ArrayList<Item>();
        RequestCatalog rc = new RequestCatalog();
        ItemShippingStatusList issl = new ItemShippingStatusList();
        
        int[] orderItems = (int[])request.getSession().getAttribute("orderItems");
        Customer cust = (Customer)request.getSession().getAttribute("cust");
        System.out.println("Customer Name:  " + cust.getName());
        //perpare the catalog
        catalog = rc.getItems();
        
        //log the order
        String inputToLog = "==========\n";

        //writeToLog("Customer " + cust.getCustomerRef() + ":  Start Order.");
        inputToLog += "Customer " + cust.getCustomerRef() + ":  Start Order.";

        //loop thru orderItems array and create an arraylist of items
        for(int i = 0; i < orderItems.length; i++) {
            //make sure the quantity is not empty
            if(orderItems[i] > 0) {
                Item tempitem = new Item();
                tempitem.setManufacturer(catalog.get(i).getManufacturer());
                tempitem.setProduct(catalog.get(i).getProduct());
                tempitem.setPrice(catalog.get(i).getPrice());
                
                //setting the amount the user requested
                int amount = orderItems[i];
                tempitem.setQuantity(amount);
                order.add(tempitem);
                
                //log the items ordered
                //writeToLog("Customer " + cust.getCustomerRef() + " requested item: " + catalog.get(i));
                inputToLog += "Customer " + cust.getCustomerRef() + " requested item: " + catalog.get(i);
            }
        }
        
        //print out end of log for this order
        //writeToLog("Customer " + cust.getCustomerRef() + ": Order complete.");
        inputToLog += "Customer " + cust.getCustomerRef() + ": Order complete.";

        //call bopel module to process the order
        ShipGoods sg = new ShipGoods(order, cust);
        
        //get the returned list
        issl = sg.getItemShippedStatusList();
        
        //go thru the shipped and not shipped lists and log the info
        if(issl.getShippedItems() != null) {
            ArrayList<Item> shipped = issl.getShippedItems();
            for(int i = 0; i < shipped.size(); i++) {
                //writeToLog("Customer " + cust.getCustomerRef() + " - Item Shipped:  " + shipped.get(i));
                inputToLog += "Customer " + cust.getCustomerRef() + " - Item Shipped:  " + shipped.get(i);
            }
        }
        
        if(issl.getNotShippedItems() != null) {
            ArrayList<Item> notShipped = issl.getShippedItems();
            for(int i = 0; i < notShipped.size(); i++) {
                //writeToLog("Customer " + cust.getCustomerRef() + " - Item Not Shipped:  " + notShipped.get(i));
                inputToLog += "Customer " + cust.getCustomerRef() + " - Item Not Shipped:  " + notShipped.get(i);
            }
        }

        inputToLog += "\n==========";
        writeToLog(inputToLog);

        

        //set the session
        request.setAttribute("result", issl);
        

        request.getRequestDispatcher("orderComplete.jsp").forward(request, response);
        
    }

    
     private boolean writeToLog(String input)
    {

        PrintWriter outputStream = null;
        //String folder = "";

        try {

            //folder = Warehouse1WS.class.getResource(INVENTORY_FILE).toURI().toString();
            //folder = folder.substring(5, folder.length()-13);
            outputStream = new PrintWriter(new FileOutputStream("InterfaceLog.txt", true));


        } catch (FileNotFoundException ex) {

            System.out.println("Error writing to the log file for the User interface");
        }

        System.out.println("Writing to log file for the User Interface...");

        outputStream.println(input);
        outputStream.close();

        System.out.println("Finished writing...");

        return true;


    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    } 

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}
